package com.wetao.lib_common.common.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.wetao.lib_common.base.BaseListResponse;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.bean.response.AppVersionInfo;
import com.wetao.lib_common.bean.response.GetSubjectList;
import com.wetao.lib_common.bean.response.Loginbean;
import com.wetao.lib_common.common.contract.LoginContract;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.JsonUtils;
import com.wetao.lib_common.utils.ToastUtil;

import java.util.HashMap;

import okhttp3.RequestBody;

/**
 * 登录 P
 */
public class LoginPresenter extends LoginContract.Presenter {

    private Context context;

    public LoginPresenter(Context context) {
        this.context = context;
    }

    @Override
    public void getLogin(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().login(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<Loginbean>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<Loginbean>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<Loginbean>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse<Loginbean> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getLoginResult(baseResponse);
                        }else {
                            getView().getLoginError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getLoginError();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getSubjectList(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().getSubjectList(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseListResponse<GetSubjectList>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseListResponse<GetSubjectList>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseListResponse<GetSubjectList>>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseListResponse<GetSubjectList> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getSubjectListResult(baseResponse);
                        }else {
                            getView().getSubjectListFail();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        getView().getSubjectListFail();
                        super.onError(e);
                    }
                });
    }

    @Override
    public void getClientVersionInfo(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json = (JsonUtils.getmGson()).toJson(map);
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
        ServerUtils.getCommonApi().getStudentScreenClientVersion(requestBody)
                .retryWhen(new RetryWithDelay(2, 1))
                .compose(RxUtils.<BaseResponse<AppVersionInfo>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<AppVersionInfo>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<AppVersionInfo>>(context, RxErrorHandler.getInstance(), isDialog, cancelable) {
                    @Override
                    public void onNext(BaseResponse<AppVersionInfo> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getClientVersionResult(baseResponse);
                        }else {
                            getView().getClientVersionError();
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().getClientVersionError();
                        super.onError(e);
                    }
                });
    }
}
